package com.anuo.app.study.studyjava;


import com.anuo.app.modules.study.entity.Product;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring/spring-context.xml"})

/**
 * 研究状态: 常用的已完成
 * #创建匿名方法
 * #Lambda 过滤
 * #在foreach中使用
 * #回调实现
 * 参考: https://www.javatpoint.com/java-lambda-expressions
 */
public class StudyLambda {


    //Lambda 过滤
    @Test
    public void studyLambdaWithStream() {
        List<Product> list = new ArrayList<>();
        list.add(new Product( "cpu", 1298));
        list.add(new Product( "memory", 300));
        list.add(new Product( "disk", 999));

        //获取list
        List<Product> productStream = list.stream()
                .filter(e -> e.getName().equals("cpu"))
                .collect(Collectors.toList());
        productStream.forEach(
                e -> System.out.println(e.getName())
        );

        //获取一个对象
        Product product = list.stream()
                .filter(e -> "jk".equals(e.getName()))
                .findFirst()
                .orElse(null);

    }

    //创建匿名方法
    @Test
    public void studyAnonymousMethodCreate() {
        MyPrint myPrint = (String name, String msg) -> {
            return name + " say " + msg;
        };

        String msg = myPrint.showMsg("anuo", "hello");
        System.out.println(msg);
    }

    //在foreach中使用
    @Test
    public void studyLambdaInFor() {
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.forEach(
                (n) -> System.out.println(n)
        );
    }

    /**
     * 回调实现
     * 参考: https://stackoverflow.com/questions/443708/callback-functions-in-java
     */
    @Test
    public void callback() {
        Server server = new Server();
        //以前的写法
        server.register("anuo", 27, new CallBack() {
                    @Override
                    public String methodToCallBack(String userName) {
                        System.out.println("我是回调");
                        return "来自客服端";
                    }
                }
        );

        //lambda 写法
        server.register("anuo", 27, (String userName) -> {
            System.out.println("我是回调");
            return "来自客服端";
        });
    }

    private static class Server {

        public void register(String userName, int age, CallBack callback) {
            System.out.println("my name is " + userName + " and age is " + age);
            System.out.println("又执行了哈");
            String result = callback.methodToCallBack(userName);
            System.out.println(result);
        }


    }

    interface MyPrint {
        String showMsg(String name, String msg);
    }

    interface CallBack {
        String methodToCallBack(String userName);
    }
}





